package com.ethink.warehouse.service.impl;

import com.ethink.warehouse.common.entity.StockOutOrderDetail;
import com.ethink.warehouse.common.mapper.StockOutOrderDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ethink.warehouse.api.request.StockOutOrderAddRequest;
import com.ethink.warehouse.api.response.StockOutOrderAddResponse;
import com.ethink.warehouse.common.entity.StockOutOrder;
import com.ethink.warehouse.common.mapper.StockOutOrderMapper;
import com.ethink.warehouse.converter.StockOutOrderConverter;
import com.ethink.warehouse.service.StockOutOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓库出库单 Service接口实现
 *
 * @author default
 * @date 2022-12-05
 */
@Slf4j
@Service
public class StockOutOrderServiceImpl implements StockOutOrderService {

    @Autowired
    private StockOutOrderMapper stockOutOrderMapper;

    @Autowired
    private StockOutOrderDetailMapper stockOutOrderDetailMapper;

    @Autowired
    private StockOutOrderConverter stockOutOrderConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StockOutOrderAddResponse addStockOutOrder(StockOutOrderAddRequest request) {
        StockOutOrderAddResponse response = new StockOutOrderAddResponse();
        StockOutOrder stockOutOrder = new StockOutOrder();
        BeanUtils.copyProperties(request, stockOutOrder);
        stockOutOrderMapper.insert(stockOutOrder);
        response.setId(stockOutOrder.getId());
        response.setId(stockOutOrder.getId());
        // 商品明细
        request.getItems().forEach(mateItem -> {
            StockOutOrderDetail stockOutOrderDetail = new StockOutOrderDetail();
            BeanUtils.copyProperties(mateItem, stockOutOrderDetail);
            stockOutOrderDetailMapper.insert(stockOutOrderDetail);
        });
        return response;
    }

}
